package com.enhe.gck.communal;

import com.enhe.gck.enums.WeighingResultEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author 褚波
 * @Date 2024-04-16 11:34:53
 * @Description http请求公共响应
 */
@Data
@NoArgsConstructor
public class Resp<T> {

    /**
     * 请求结果  0 失败;1 成功
     */
    private Integer code;

    /**
     * 消息说明
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    public Resp(ResultCode resultCode, String message) {
        this.code = resultCode.getCode();
        this.message = message;
    }

    public Resp(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Resp(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Resp<T> success() {
        return success(null, null);
    }

    public static <T> Resp<T> success(String message) {
        return success(message, null);
    }

    public static <T> Resp<T> success(T data) {
        return success(null, data);
    }

    public static <T> Resp<T> success(String message, T data) {
        return new Resp<T>(ResultCode.ONE.getCode(), message, data);
    }

    public static <T> Resp<T> fail() {
        return fail(null);
    }

    public static <T> Resp<T> fail(String message) {
        return new Resp<>(ResultCode.ZERO.getCode(), message);
    }

    public static <T> Resp<T> fail(String message, T data) {
        return new Resp<>(ResultCode.ZERO.getCode(), message, data);
    }

    public static <T> Resp<T> weighFail(WeighingResultEnum resultEnum) {
        return new Resp<>(resultEnum.getCode(), resultEnum.getName());
    }
    public static <T> Resp<T> weighFail(Integer code, String message) {
        return new Resp<>(code, message);
    }
    public static <T> Resp<T> weighFail(WeighingResultEnum resultEnum, T data) {
        return new Resp<>(resultEnum.getCode(), resultEnum.getName(), data);
    }
    public static <T> Resp<T> weighFail(Integer code, String message, T data) {
        return new Resp<>(code, message, data);
    }

    public static <T> Resp<T> mysqlFail(String message) {
        return new Resp<>(ResultCode.Mysql_Exception.getCode(), message);
    }
}
